﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pigmalion.BusinessEntities;
using Pigmalion.Presenter.ProductoPresenter;

namespace Pigmalion.UIWF.Almacen
{
    public partial class ProductoView : Form,IProductoView,IReceiver
    {
        ProductoPresenter presenter;

        public ProductoView()
        {
            InitializeComponent();

            presenter = new ProductoPresenter(this);
            
        }

       

        public List<Producto> GetAllFromProductos
        {
            get { return productoBindingSource.DataSource as List<Producto>;}
            set { productoBindingSource.DataSource = value; }
        }

        public Producto CurrentProducto
        {
            get { return productoBindingSource.Current as Producto; }
            set { productoBindingSource.DataSource = value; }
        }

        private void ProductoView_Load(object sender, EventArgs e)
        {
            presenter.CargarProducto();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                presenter.BuscarProductoByCriterio(txtCriterio.Text);
            }

            if (e.KeyCode == Keys.Down)
            {
                pigmalionDataGridView1.Focus();
            }
        }


        public void New()
        {
            ProductoMttoView productoMttoView = new ProductoMttoView();
            productoMttoView.NewProduct();
            productoMttoView.ShowDialog();
        }

        public void Edit()
        {
            ProductoMttoView productoMttoView = new ProductoMttoView();
            productoMttoView.CurrentProduct = productoBindingSource.Current as Producto;
            productoMttoView.EditProduct();
            productoMttoView.ShowDialog();
        }

        public void Delete()
        {
           DialogResult result= MessageBox.Show(this,"Desea Eliminar el Registro Seleccionado","Eliminar Registro",MessageBoxButtons.YesNo,MessageBoxIcon.Information);

           if (result == DialogResult.Yes)
           {
               productoBindingSource.RemoveCurrent();
           }
        }

        public void RefreshData()
        {
            presenter.CargarProducto();
        }

        private void botonBuscar_Click(object sender, EventArgs e)
        {
            presenter.BuscarProductoByCriterio(txtCriterio.Text);
        }

        private void botonSeleccionar_Click(object sender, EventArgs e)
        {
            CurrentProducto = productoBindingSource.Current as Producto;
            this.Close();
        }


        public int ProgressPercentage
        {
            set {toolStripProgressBar1.Value = value;}
        }

        private void pigmalionDataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (botonSeleccionar.Visible)
            {
                if (e.KeyCode == Keys.F5)
                {
                    botonSeleccionar_Click(sender, e);
                }
            }

            if (e.KeyCode == Keys.ControlKey)
            {
                txtCriterio.Focus();
            }
        }


        public string Time
        {
            set { lblTime.Text = value; }
        }
    }
}
